// pages/mainCourse/mainCourse.js
import api from '../../utils/api.js'
import { config } from '../../utils/config'
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        list: [],
        status: 0,
        checkAll: true,
        show: true,
        count: 0,
        show1: false,
        current: 0,
        count: 0, //食材总数
        count1: 0, //调料总数
        select: [],
        imgUrl: config.imgUrl,
    },

    toindex() {
        wx.switchTab({
            url: "/pages/index/index"
        })
    },
    count() {
        var count = 0
        var count1 = 0
        wx.getStorageSync("select-menus").map((item) => {
            if (item.flag) {
                count += item.count * 1
                count1 += item.seasoning_count * 1
            }
        })
        this.setData({
            count,
            count1
        })
    },
    //单选
    check(e) {
        var newList = this.data.select;
        var flag = newList.filter((item) => {
            return item.id == e.target.dataset.id
        })[0].flag;
        newList.filter((item) => {
            return item.id == e.target.dataset.id
        })[0].flag = !flag;
        var checkAll = 1;
        for (var i = 0; i < newList.length; i++) {
            if (newList[i].flag == 0) {
                checkAll = 0;
            }
        }
        wx.setStorageSync("select-menus", newList);
        this.setData({
            select: newList,
            checkAll
        })
        this.count()
    },
    //全选
    checkAll() {
        var checkAll = !this.data.checkAll
        var newList = this.data.select
        newList.map((item) => {
            item.flag = checkAll
        })
        wx.setStorageSync("select-menus", newList);
        this.setData({
            select: newList,
            checkAll
        });
        this.count()
    },
    //删除商品
    del(e) {
        var that = this
        wx.showModal({
            title: '',
            content: '删除选中商品',
            success(res) {
                if (res.confirm) {
                    var newList = that.data.select.filter((item) => {
                        return item.id != e.target.dataset.id
                    })
                    wx.setStorageSync("select-menus", newList);
                    that.setData({
                        select: newList
                    });
                    if (newList.length == 0) {
                        that.setData({
                            show: false
                        })
                    }
                    that.count()
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    zhifu() {
        if (this.data.show) {
            var newList = this.data.select.filter((item) => {
                return item.flag
            })
            wx.setStorageSync("select-menus", newList);
            if (newList.length > 0) {
                wx.navigateTo({
                    url: "/pages/recommendShopList/recommendShopList?type=1"
                })
            } else {
                wx.showToast({
                    title: '请勾选您要吃的菜！',
                    icon: "none"
                })
            }
        } else {
            wx.showToast({
                title: '您还未选菜哦！',
                icon: "none"
            })
        }

    },
    //添加菜谱
    add(e) {
        http.request({
            url: api.shoppingCartAdd,
            method: "POST",
            data: {
                user_id: wx.getStorageSync("user_id"),
                product_id: e.target.dataset.id
            },
            success: (res) => {
                var num = this.data.num * 1 + 1
                this.setData({
                    num
                })
                wx.hideLoading()
                if (res.code == 200) {
                    wx.showToast({
                        title: '添加成功',
                        duration: 1500
                    })
                };

                if (num) {
                    wx.setTabBarBadge({
                        index: 3,
                        text: num.toString()
                    })
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log('options', options)

        // 加载菜篮时 清除缓存 时间段
        wx.removeStorageSync('timeField1')
        wx.removeStorageSync('timeField2')

        if (options.loopType == 1) { //从 菜店循环中 返回到 已选菜单页面 
            console.log('从 菜店循环中 返回到 已选菜单页面 ')
            let selectedMenus = wx.getStorageSync('select-menus')
            let alreadyMenusId = wx.getStorageSync('alreadyMenusId')
            wx.removeStorageSync('alreadyMenusId') //再清除 已购买过的菜单id
            selectedMenus.forEach((ele, i) => {
                if (alreadyMenusId) {
                    alreadyMenusId.forEach((sEle) => {
                        if (sEle == ele.id) { //删除已购买的 菜谱
                            selectedMenus.splice(i, 1)
                        }
                    })
                }

            })
            wx.setStorageSync("select-menus", selectedMenus);
            this.setData({
                select: selectedMenus,
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            show1: false
        })
        //获取已选菜谱的列表
        var select = wx.getStorageSync("select-menus");
        select.map((item) => {
            item.flag = true
        })
        wx.setStorageSync("select-menus", select);
        this.setData({
            select,
            checkAll: true,
        })
        this.count()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }
})